﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Xml;
using SplendidCRM.Ext.CCBackendService;
using SplendidCRM.Ext.Helpers;
using SplendidCRM.Ext.Models;

namespace SplendidCRM.Ext.Controllers
{
 
    public class BackEndSystemController: Controller
    {
        [HttpGet]
        public ActionResult QueryList()
        {
            return View();
        }

        [HttpPost]
        public ActionResult QueryList(PhoneNumberModel model)
        {
            //var service = new CCBackendService.Service();
            //var result = service.QueryBalance(model.PhoneNumber);

            var result = new BalanceRecordType[2]
                             {
                                 new BalanceRecordType() {ExpireTime = Guid.NewGuid().ToString(), BalanceDesc = "456"},
                                 new BalanceRecordType() {ExpireTime = Guid.NewGuid().ToString(), BalanceDesc = "skdfhskdhf"}
                             };
            return PartialView("QueryBalanceResult", result);
        }

        
        [HttpGet]
        public ActionResult LoadServiceForm(string serviceName)
        {
            var viewName = serviceName;
            object model = null;
            var defaultPhoneNumber = Manager.Session.LattestPhoneNumber;

            if (PhoneNumberModel.ViewNames.Contains(serviceName))
            {
                if (serviceName.Equals(PhoneNumberModel.CheckScratchCard))
                {
                    defaultPhoneNumber = string.Empty;
                }
                viewName = "PhoneNumber";
                return PartialView(viewName + "View", new PhoneNumberModel(){ServiceName = serviceName,PhoneNumber = defaultPhoneNumber});
            }
            
            if (serviceName.Equals(QueryHistoryOfPaymentModel.ViewName))
            {
                model = new QueryHistoryOfPaymentModel(){PhoneNumber = defaultPhoneNumber};
            }

            else  if (serviceName.Equals(SearchPINPUKModel.ViewName,StringComparison.CurrentCultureIgnoreCase))
            {
                model = new SearchPINPUKModel();
            }
            else if (serviceName.Equals(ImportPinPukModel.ViewName))
            {
                model = new ImportPinPukModel()
                            {
                                RunDate = DateTime.Now.ToString("dd/MM/yyyy"),
                                RunTime = "235900",
                                PinPukFilename = @"D:\FTP_Data\PINPUKRawFile\"
                            };
            }
            else if (serviceName.Equals(ShowImportLogModel.ViewName))
            {
                model = new ShowImportLogModel();
            }

            return PartialView(viewName + "View", model);
        }

        public ActionResult ShowImportLog( ShowImportLogModel model )
        {
            var service = new CCBackendService.Service();
            var result = service.GetImportLog(model.FromDate, model.ToDate);
            return PartialView("ShowImportLogResultView", ToDataSet(result));
        }

        [HttpPost]
        public ActionResult PhoneNumberServiceQuery(PhoneNumberModel model)
        {
            var service = new CCBackendService.Service();
            object result = null;

            if (model.ServiceName.Equals(PhoneNumberModel.ViewNames[0]))
            {
               result = service.GetPostpaidBalance(model.PhoneNumber);
            }
            else if (model.ServiceName.Equals(PhoneNumberModel.ViewNames[1]))
            {
                result = service.QueryBalance(model.PhoneNumber);
            }
            else if (model.ServiceName.Equals(PhoneNumberModel.ViewNames[2]))
            {
                result = service.QueryBasicInfo(model.PhoneNumber);
            }
            else if (model.ServiceName.Equals(PhoneNumberModel.ViewNames[3]))
            {
                result = service.CheckScratchCard(model.PhoneNumber);
            }
            else if (model.ServiceName.Equals(PhoneNumberModel.ViewNames[4]))
            {
                result = service.IntegrationEnquiry(model.PhoneNumber);
            } 
            return PartialView(model.ServiceName+ "ResultView", result);
        }

        [HttpPost]
        public ActionResult QueryHistoryOfPayment(QueryHistoryOfPaymentModel model)
        {
            var service = new CCBackendService.Service();
            var result = service.QueryHistoryOfPayment(model.PhoneNumber,model.StartDate,model.EndDate);

            return PartialView("QueryHistoryOfPaymentResultView",result);
        }

        [HttpPost]
        public ActionResult SearchPinPukQuery(SearchPINPUKModel model)
        {
            var service = new Service();
            var result = service.SearchPINPUK(model.ICCID??string.Empty,model.IMSI??string.Empty);
            
            return PartialView("SearchPinPukQueryResultView", ToDataSet(result));
        }

        private DataSet ToDataSet(string content)
        {
            var ds = new DataSet();
            ds.ReadXml(new XmlTextReader(new StringReader(content)));
            return ds;
        }

        [HttpPost]
        public ActionResult ImportPinPuk(ImportPinPukModel model)
        {
            var result = false;
            if (Manager.Site.IsAdmin)
            {
                var service = new CCBackendService.Service();
                //var dateInput = model.RunDate != null ? model.RunDate.Replace("/", "") : string.Empty;
                
                DateTime date;
                var isValid = DateTime.TryParseExact(model.RunDate,"dd/MM/yyyy", null, DateTimeStyles.NoCurrentDateDefault, out date);
                if ( isValid)
                {
                    var dateInput = date.ToString("yyyyMMdd");
                    result = service.ImportPINPUK(dateInput, model.RunTime, model.PinPukFilename);
                }    
            }
            return PartialView("ImportPinPukQueryResultView", result);
        }

    }
}

